package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.MyView.ContainsEmojiEditText;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class EditJianjieActivity extends BaseActivity {

    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.updataok)
    TextView updataok;
    @BindView(R.id.jianjietext)
    ContainsEmojiEditText jianjietext;
    @BindView(R.id.jianjienum)
    TextView jianjienum;
    private int type = 0;//0是从创建过来的，1是从编辑过来的
    private String bookId;
    private NoBackgLodDialog loadialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_jianjie);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            bookId = getIntent().getStringExtra("bookId");
            jianjietext.setText(getIntent().getStringExtra("jianjietext"));
            jianjienum.setText(jianjietext.getText().toString().length() + "/200");
        }
        if (type==3){
            jianjietext.setText(getIntent().getStringExtra("jianjietext"));
            jianjienum.setText(jianjietext.getText().toString().length() + "/200");
        }
        jianjietext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().length() >= 200) {
                    showToast("字数不能超过200哦");
                }
                jianjienum.setText(editable.toString().length() + "/200");
            }
        });
    }

    @OnClick({R.id.cancel, R.id.updataok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                goBack();
                break;
            case R.id.updataok:
                if (StringTool.isEmpty(jianjienum.getText().toString())) {
                    showToast("请输入简介");
                    return;
                }
                if (type!=1){
                    Intent intent=new Intent();
                    intent.putExtra("jianjietext",jianjietext.getText().toString());
                    setResult(5,intent);
                    goBack();
                    return;
                }
                toJianjie();
                break;
        }
    }

    private void toJianjie() {
        OkHttpUtils.post(HttpManager.IP + "/book-update")

                .params("bookId", bookId)
                .params("bookIntroduction", jianjietext.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (loadialog == null) {
                            loadialog = new NoBackgLodDialog(EditJianjieActivity.this, true);
                        }
                        loadialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (loadialog != null) {
                            loadialog.dismis();
                        }
                        try {
                            JSONObject js = new JSONObject(s);
                            if (js.getInt("returnCode") == 200) {
                                showToast(js.getString("msg"));
                                Intent intent=new Intent();
                                intent.putExtra("jianjietext",jianjietext.getText().toString());
                                setResult(5,intent);
                                goBack();
                            }else {
                                if (js.getInt("returnCode") == 400){
                                    Intent intent=new Intent(EditJianjieActivity.this,LoginActivity.class);
                                    startActivity(intent);
                                }
                                showToast(js.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
